//
// Created by 刘禹晨 on 16/5/25.
// Copyright (c) 2016 刘禹晨. All rights reserved.
//

#import "SettingViewController.h"
#import <SDAutoLayout/SDAutoLayout.h>
#import "ViewController.h"

#define MaxWidth [UIScreen mainScreen].bounds.size.width
#define MaxHeight [UIScreen mainScreen].bounds.size.height

@interface SettingViewController() <UITextFieldDelegate>
{
    UITextField *_socketHostField;
    UITextField *_socketPortField;
    UILabel *_socketHostLabel;
    UILabel *_socketPortLabel;
    UIButton *_submitButton;
    NSUserDefaults *_defaults;
    ViewController *mainVC;
}


@end


@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //[self.view setBackgroundColor:[[UIColor new] initWithRed:239.0f/255.0f green:239.0f/255.0f blue:244.0f/255.0f alpha:1.0f]];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    _defaults = [NSUserDefaults standardUserDefaults];
    [self initSocketHostField];
    [self initSocketPortField];
    [self initButton];
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    self.navigationItem.title = @"WiFi设置";
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style: UIBarButtonItemStylePlain target:self action:@selector(backToViewController:)];
    self.navigationItem.leftBarButtonItem = backButton;
}


-(void)viewWillAppear:(BOOL)animated
{
    NSString *defaultSocketHost = [_defaults objectForKey:@"socketHost"];
    _socketHostField.text = defaultSocketHost;
    NSString *defaultSocketPort = [NSString stringWithFormat:@"%@",[_defaults objectForKey:@"socketPort"]];
    _socketPortField.text = defaultSocketPort;
}

-(void)initSocketHostField
{
    _socketHostLabel = [[UILabel alloc]init];
    _socketHostLabel.text = @"协调器IP地址";
    _socketHostLabel.textColor = [UIColor grayColor];
    _socketHostLabel.font = [UIFont systemFontOfSize:14];
    CGFloat height = [_socketHostLabel.text sizeWithAttributes:@{NSFontAttributeName : _socketHostLabel.font}].height;
    CGFloat width = [_socketHostLabel.text sizeWithAttributes:@{NSFontAttributeName : _socketHostLabel.font}].width;
    [self.view addSubview:_socketHostLabel];
    _socketHostField = [[UITextField alloc]init];
    _socketHostField.delegate = self;
    CGRect frame =  _socketHostField.frame;
    frame.size.width = 7.0f;
    UIView *leftview = [[UIView alloc] initWithFrame:frame];
    _socketHostField.leftViewMode = UITextFieldViewModeAlways;
    _socketHostField.leftView = leftview;
    _socketHostField.layer.borderWidth = 2.0f;
    _socketHostField.layer.cornerRadius = 5;
    _socketHostField.layer.borderColor = [[UIColor new] initWithRed:255.0f/255.0f green:73.0f/255.0f blue:64.0f/255.0f alpha:1.0f].CGColor;
    [_socketHostField setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:_socketHostField];
    _socketHostLabel.sd_layout.leftSpaceToView(self.view,10).widthIs(width).heightIs(height).topSpaceToView(self.view,16);
    _socketHostField.sd_layout.leftEqualToView(_socketHostLabel).topSpaceToView(_socketHostLabel,5).rightSpaceToView(self.view,10).heightIs(38);
}

-(void)initSocketPortField
{
    _socketPortLabel = [[UILabel alloc]init];
    _socketPortLabel.text = @"协调器端口";
    _socketPortLabel.textColor = [UIColor grayColor];
    _socketPortLabel.font = [UIFont systemFontOfSize:14];
    CGFloat height = [_socketPortLabel.text sizeWithAttributes:@{NSFontAttributeName : _socketPortLabel.font}].height;
    CGFloat width = [_socketPortLabel.text sizeWithAttributes:@{NSFontAttributeName : _socketPortLabel.font}].width;
    [self.view addSubview:_socketPortLabel];
    _socketPortField = [[UITextField alloc]init];
    _socketPortField.delegate = self;
    CGRect frame = _socketPortField.frame;
    frame.size.width = 7.0f;
    UIView *leftview = [[UIView alloc] initWithFrame:frame];
    _socketPortField.leftViewMode = UITextFieldViewModeAlways;
    _socketPortField.leftView = leftview;
    _socketPortField.layer.borderWidth = 2.0f;
    _socketPortField.layer.cornerRadius = 5;
    _socketPortField.layer.borderColor = [[UIColor new] initWithRed:255.0f/255.0f green:73.0f/255.0f blue:64.0f/255.0f alpha:1.0f].CGColor;
    [_socketPortField setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:_socketPortField];
    _socketPortLabel.sd_layout.leftSpaceToView(self.view,10).widthIs(width).heightIs(height).topSpaceToView(_socketHostField,16);
    _socketPortField.sd_layout.leftEqualToView(_socketHostLabel).topSpaceToView(_socketPortLabel,5).rightSpaceToView(self.view,10).heightIs(38);

}

-(void)initButton
{
    _submitButton = [[UIButton alloc]init];
    [_submitButton setTitle:@"保存" forState:UIControlStateNormal];
    _submitButton.backgroundColor = [[UIColor new] initWithRed:255.0f/255.0f green:73.0f/255.0f blue:64.0f/255.0f alpha:1.0f];
    _submitButton.clipsToBounds = YES;
    _submitButton.layer.cornerRadius = 5;
    [_submitButton addTarget:self action:@selector(saveConnectInfo:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_submitButton];
    _submitButton.sd_layout.leftSpaceToView(self.view,10).rightSpaceToView(self.view,10).topSpaceToView(_socketPortField,24).heightIs(38);

}

-(void)saveConnectInfo:(id)sender
{
    NSString *socketHost = _socketHostField.text;
    NSString *socketPortString = _socketPortField.text;
    NSInteger socketPortInt = [socketPortString intValue];
    [_defaults setObject:socketHost forKey:@"socketHost"];
    [_defaults setInteger:socketPortInt forKey:@"socketPort"];
    [_defaults synchronize];
    if(mainVC == nil){
        mainVC = [self.navigationController.viewControllers objectAtIndex:0];
    }
    [self.navigationController popToViewController:mainVC animated:YES];
    [mainVC viewDidAppear:YES];
}

-(void)backToViewController:(id)sender
{
    if(mainVC == nil){
        mainVC = [self.navigationController.viewControllers objectAtIndex:0];
    }
    [self.navigationController popToViewController:mainVC animated:YES];
    [mainVC viewDidAppear:YES];

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [_socketHostField resignFirstResponder];
    [_socketPortField resignFirstResponder];
    return YES;
}

@end